package com.z.test;

import java.util.Arrays;

public class SelectionSort {
    private static int[] arr = new int[]{1,4,3,5,6,2};

    // 选择排序
    public static void selection_sort(int[] arrs){
        int[] arr = Arrays.copyOf(arrs,arrs.length);
        int len = arr.length;
        System.out.println("选择排序 selection sort:");
        printArrs("原始：", arr);
        int minIndex,temp;
        for(int i = 0; i < len - 1; i++){
            minIndex = i;
            System.out.println("第" + (i+1) + "轮");
            for(int j = i + 1; j < len; j++){
                System.out.println("比较：arr[" + j + "]和arr[" + minIndex + "] " + arr[j] + "和" + arr[minIndex]);
                if(arr[j] < arr[minIndex]){
                    minIndex = j;
                }
            }
            printArrs("之前：" , arr);
            if(i != minIndex){
                temp = arr[i];
                arr[i] = arr[minIndex];
                arr[minIndex] = temp;
                System.out.println("交换arr[" + i + "]和arr[" + minIndex + "] " + arr[i] + "和" + arr[minIndex]);

            }
            printArrs("之后：" , arr);
        }
        printArrs("最终：" , arr);
    }

    public static void main(String[] args) {
        selection_sort(arr);
    }







    static void printArrs(String preStr , int[] arr){
        String str = "";
        for(int i=0;i<arr.length;i++){
            str += arr[i] + ",";
        }
        System.out.println(preStr + str.substring(0,str.length()-1));
    }
}
